﻿package emusic.ws 
{
	import flash.events.AsyncErrorEvent;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class AbstractProxyClient extends EventDispatcher
	{
		protected var _requestQueue:Array;
		protected var _requestId:Number = 0;		
		protected var _conn:NetConnection;		
		protected var _proxyUrl:String = "http://www.martinlegris.com/amf3/gateway.php";
		
		public function get proxyUrl():String { return _proxyUrl; }
		public function set proxyUrl(value:String):void { _proxyUrl = value; }
		
		public function AbstractProxyClient() 		
		{
			_requestQueue = [];
			
			_conn = new NetConnection();
			_conn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
			_conn.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
			_conn.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
			_conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
			_conn.connect(_proxyUrl);
		}
		
		public function getByUrl(url:String):Number
		{
			var responder:Responder = new Responder(doUrlSuccess, doUrlFault);
			var wrapper:Object = getWrapper(responder, "getByUrl");
			call(responder, url, "GET", [], [], [], wrapper.id);			
			return wrapper.id;
		}
		
		protected function doUrlSuccess(result:Object):void
		{
			var requestId:Number = parseInt(result.requestId);
			var wrapper:Object = getWrapperById(requestId);
			var httpCode:String = result.httpCode;
			var content:String = result.content;

			if (httpCode == "200")
			{
				
				trace("content:" + content);				
			}
			else
			{
				trace("error:" + httpCode);
			}
		}
		
		protected function doUrlFault(fault:Object):void
		{
			trace("fault:" + fault);
		}
		
		protected function call(responder:Responder, url:String, method:String, getVars:Array, postVars:Array, headers:Array, requestId:Number):void
		{
			_conn.call("RESTProxy.request", responder, url, method, getVars, postVars, headers, requestId);
		}
		
		protected function getRequestById(id:Number):Object
		{
			var i:Number = 0;
			for (i = 0; i < _requestQueue.length; i++)
			{
				if (_requestQueue[i].id == id)
					return _requestQueue[i];
			}
			
			return null;
		}
		
		protected function getWrapper(responder:Responder, type:String):Object
		{
			var wrapper:Object = { };
			wrapper.id = _requestId++;
			wrapper.success = false;
			wrapper.type = type;
			wrapper.responder = responder;
			_requestQueue.push(wrapper);
			
			return wrapper;
		}
		
		protected function traceObj(obj:Object):void
		{
			var i:String;
			for (i in obj)
				trace(i + ":" + obj[i]);
		}
		
		protected function getWrapperById(requestId:Number):Object
		{
			for (var i:Number = 0; i < _requestQueue.length; i++)
			{
				if (_requestQueue[i].id == requestId)
					return _requestQueue[i];
			}
			
			trace("wrapper with requestId:" + requestId + " not found. Returning null");
			return null;
		}
		
		protected function getWrapperFromResult(result:Object):Object
		{
			var requestId:Number = parseInt(result.requestId);
			return getWrapperById(requestId);
		}
		
		protected function doAsyncError(evt:AsyncErrorEvent):void
		{
			
		}
		
		protected function doNetStatus(evt:NetStatusEvent):void
		{
			
		}
		
		protected function doIOError(evt:IOErrorEvent):void
		{
			
		}
		
		protected function doSecurityError(evt:SecurityErrorEvent):void
		{
			
		}
		
		protected function doFault(fault:Object):void
		{
			
		}
	}
}